<?php
/**
 * db_mysqli
 * mysql操作基类
 */
die();
//defined('IN_LK') or die('Access Denied');
final class db_mysqli
{
	/**
     * 数据库连接id
     */
	public $connid;
	
	public static $mysqli = null;
	
    /**
     * sql执行次数
	 */
	public $querynum = 0;
	
    /**
     * 当前数据库名称
	 */
	public $dbname;
	
	public function __construct()
	{
	}
	
	public function connect($db_host = DB_HOST, $db_user = DB_USER, $db_pass = DB_PASS, $db_name = DB_NAME, $db_pcon = 0, $db_char = '')
	{
		if(!is_object(self::$mysqli))
		{
			self::$mysqli = new mysqli();
		}
		if(!$this->connid = @self::$mysqli->connect($db_host, $db_user, $db_pass))
		{
			self::halt($this->error());
		}
		/*if($this->version()> '4.1')
		{
			$char = $db_char ? 
			"character_set_connection='$db_char',character_set_results='$db_char',character_set_client=binary" : '';
			$char .= $this->version()> '5.0.1' ? (empty($char) ? '' : ',') . 'sql_mode=""' : '';
			$char && mysql_query("SET {$char}", $this->connid);
		}
		
		if($db_name && !@mysql_select_db($db_name, $this->connid))
		{
			$this->halt(mysql_error());
		}*/
		$this->dbname = $db_name;
		return $this->connid;
	}
	
	private function error()
	{
		return $this->connid ? $this->connid->$error : mysqli_connect_error();
	}
	
	private static function halt($string)
	{
		echo $string;
		exit;
	}
}

//数据库相关设置
define('DB_HOST', 'localhost');  //主机
define('DB_USER', 'root');  //用户名
define('DB_PASS', 'root');  //数据库密码
define('DB_NAME', 'jiajiao');  //数据库名称
define('DB_PRE', 'j_');  //数据表前缀
define('DB_CHAR', 'utf8');  //数据库字符集
define('DB_TYPE', 'mysql');  //数据库类型
define('DB_PCON', 0);  //是否使用持久连接0不使用，仅对mysql类型链接有
$db = new db_mysqli();
$db->connect();
$db = new db_mysqli();
$db->connect();